import {Component, OnInit} from '@angular/core';
import {App, NavController} from 'ionic-angular';
import {PersonPage} from '../person/person';
import {services} from "../../providers/service";
import {ModifyPswService} from "./modifyPsw.service";
import {UserService} from "../../providers/user-service";
import {AppPopup} from "../../util/popup";
import {UserLogin} from "../../modules/app-user/user-login/user-login";

@Component({
    selector: 'page-modifyPsw', templateUrl: 'modifyPsw.html', providers: [ModifyPswService]
})
export class ModifyPswPage implements OnInit {
    timer: any;

    hasClick = false;
    showAlert = true;  //
    num = 120;
    errorMsg;   //校验
    userInfo = {
        "phone": "", "yzm": "", "newPassWord": "", "rePassWord": ""
    }
    xgcg; //修改密码成功的返回信息

    constructor(public navCtrl: NavController,
                public service: services,
                private userServ: UserService,
                private app: App,
                private popup: AppPopup,
                public modifyPswService: ModifyPswService,) {

    }

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });

        if (this.service.startTime) {
            let endTime = new Date().getTime();
            if (Math.floor((endTime - this.service.startTime) / 1000) < 120) {
                this.num = 120 - Math.floor((endTime - this.service.startTime) / 1000);
                this.hasClick = true;
                this.timer = setInterval(() => this.doLoop(), 1000);
            } else {
                this.service.startTime = null;
            }
        }
    }

    goback() {
        clearInterval(this.timer);
        this.navCtrl.pop(PersonPage);
    }

    //获取修改密码的验证码（需要登录）
    sendMunber() {
        let param = {}

        this.modifyPswService.editPasswordCode(param)
            .then(data => {
                console.log("发送验证码成功");
                if (data.resultStatus == 'success') {
                    this.hasClick = true;
                    this.timer = setInterval(() => {
                        this.doLoop();
                    }, 1000)

                } else {
                    this.errorMsg = data.errorMassage;
                }
            }, err => {
                console.log('失败回调');
                return err;
            })
            .catch(fail => {
                console.log('异常了', fail);
            });

    }

    toEditPassword() {
        var pwdTest = /^(?![\d]+$)(?![a-zA-Z]+$)(?![-!@#$%^&*()_+.{}`=|\/\[\]\\\'?;\':,<>]+$)[\da-zA-Z-!@#$%^&*()_+.{}`=|\/\[\]\\\'?;\':,<>]{6,32}$/;
        if (this.userInfo.yzm == '') {
            this.errorMsg = "验证码不能为空";
        } else if (this.userInfo.newPassWord.length < 6 || this.userInfo.newPassWord.length > 32) {
            this.errorMsg = "密码长度必须在6-32位之间！";
        } else if (!pwdTest.test(this.userInfo.newPassWord) || !pwdTest.test(this.userInfo.rePassWord)) {
            this.errorMsg = "密码只能由字母、数字、特殊字符的两种或三种组合成！";
        } else if (this.userInfo.newPassWord != this.userInfo.rePassWord) {
            this.errorMsg = "两次输入的密码不一致";
        } else {
            let param = {
                "password": this.userInfo.newPassWord, "verificationCode": this.userInfo.yzm
            }
            this.modifyPswService.editPassword(param)
                .then(data => {
                    if (data.resultStatus == 'success') {
                        this.showAlert = false;
                        this.xgcg = data.infoStr;
                        this.service.startTime = null;
                        clearInterval(this.timer);
                    } else {
                        this.errorMsg = data.errorMassage;
                    }
                }, err => {
                    console.log('失败回调');
                    return err;
                })
                .catch(fail => {
                    console.log('异常了', fail);
                });
        }

    }

    btnChoose() {
        this.showAlert = true;
        this.navCtrl.pop(PersonPage);
    }

    ngOnInit() {
        this.userInfo.phone = this.service.personalDetail.mobile;
        console.log(this.userInfo.phone);
    }

    doLoop() {
        if (!this.service.startTime) {
            this.service.startTime = new Date().getTime();
        }
        if (this.num == 0) {
            clearInterval(this.timer);
            this.hasClick = false;
            this.service.startTime = null;
            this.num = 120;
            return;
        }
        this.num--;
    }

    tstClock() {
        console.log(this.service.startTime);
        this.hasClick = true;
        this.timer = setInterval(() => this.doLoop(), 1000)
    }

}
